#include "file.h"
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

FILE *file_open(const char *fpath, const char *mode)
{
    errno = 0;
    FILE *f = fopen(fpath, mode);
    if(!f)
    {
        fprintf(stderr, "ERROR: could not open file %s: %s\n", fpath, strerror(errno));
        exit(1);
    }
    if(mode[0] == 'r' && !access(fpath, R_OK))
    {
        fprintf(stderr, "ERROR: cannot read from file %s: %s\n", fpath, strerror(errno));
        exit(1);
    }

    if(mode[0] == 'w' && !access(fpath, W_OK))
    {
        fprintf(stderr, "ERROR: cannot write to file %s: %s\n", fpath, strerror(errno));
        exit(1);
    }

    return f;
}
